package labmanager.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class AccessDB {

	private static final Logger LOGGER = Logger.getLogger(AccessDB.class.getName());

	private String url = "jdbc:mysql://localhost/labmanager";
	private String user = "root";
	private String pass = "root";

	/**
	 * Metodo que realiza la conexion a la base de datos
	 * 
	 * @return
	 */
	public Connection connect() {
		Connection link = null;
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			link = DriverManager.getConnection(this.url, this.user, this.pass);
			return link;

		} catch (Exception ex) {
			LOGGER.debug("No es posible realiazar la conexion a la base de datos.", ex);
		}
		return link;
	}

	/**
	 * Metodo que realiza consultas a la base de datos
	 * 
	 * @param query
	 * @return
	 */
	public ResultSet executeQuery(String query) {
		LOGGER.debug(query);
		AccessDB mysql = new AccessDB();
		java.sql.Connection cn = mysql.connect();
		if (cn != null) {
			try {
				Statement instrution = cn.createStatement();
				ResultSet result = instrution.executeQuery(query);
				return result;
			} catch (SQLException ex) {
				LOGGER.debug(new StringBuilder("Error de acceso a la base de datos ").append(ex).toString(), ex);
			}
		}
		return null;
	}

	/**
	 * Metodo que realiza insert, update and delete
	 * 
	 * @param query
	 * @return
	 */
	public int executeQueryDML(String query) {
		LOGGER.debug(query);
		AccessDB mysql = new AccessDB();
		java.sql.Connection cn = mysql.connect();
		if (cn != null) {
			try {
				Statement instrution = cn.createStatement();
				int result = instrution.executeUpdate(query);
				return result;
			} catch (SQLException ex) {
				LOGGER.debug(new StringBuilder("Error de acceso a la base de datos ").append(ex).toString(), ex);
			}
		}
		return 0;
	}

	public static void main(String args[]) throws SQLException {

//		 Test crear o actulizar usuario

//		 LabManagerUser labmanager = new LabManagerUser();
//		 ResponseDTO x = labmanager.updateUser(39647612, "Alison", "Perez",
//		 "aperez", "45676", 1, 2);
//		  Test login

//		 LabManagerUser labmanager = new LabManagerUser();
//		 ResponseDTO x = labmanager.loginUser("jpiram",
//		 "81dc9bdb52d04dc20036dbd8313ed055");
//		 System.out.println(x.getMessage() + x.getCode());
//		 Test listar usuarios
		
//		LabManagerTypeReservation labmanager = new LabManagerTypeReservation();
//		ArrayList<String> x = labmanager.listTypeReservation("1 = 1");
//		for (int i = 0; i < x.size(); i++) {
//			System.out.println(x.get(i));
//		}
		
//		LabManagerReservation labmanager = new LabManagerReservation();
//		ResponseDTO x = labmanager.updateReservation(2, "1986/01/09 06:00", 0, 0, "Aceptada");
//		System.out.println(x.getMessage() + x.getCode());
	}

}
